Crate assertables[][src]

Expand description

Assertables: Rust crate of macros assert, assume, assure

This assertables Rust crate provides macros assert…!, assume…!, assure…!, all for runtime reliability checking, and all described below. By SixArm.com.

Crate: https://crates.io/crates/assertables

Docs: https://docs.rs/assertables/

Repo: https://github.com/sixarm/assertables-rust-crate/

Introduction

This Rust crate provides macros for Rust runtime checking.

Examples:

  • assert_lt!(1, 2) means check that 1 is less than 2, otherwise panic.

  • assume_lt!(1, 2) means check that 1 is less than 2, otherwise return error.

  • assure_lt!(1, 2) means check that 1 is less than 2, otherwise return false.

Assert

Example to assert that x is less than y:

assert_lt!(1, 2);
//-> ()
assert_lt!(2, 1);
//-> panic!("assertion failed: `assert_lt(left, right)`\n  left: `2`\n right: `1`")

Assume

Example to assume that x is less than y:

let x = assume_lt!(1, 2);
//-> Ok(true)
let x = assume_lt!(2, 1);
//-> Err("assumption failed: `assume_lt(left, right)`\n  left: `2`,\n right: `1`")

Assure

Example to assure that x is less than y:

let x = assure_lt!(1, 2);
//-> Ok(true)
let x = assure_lt!(2, 1);
//-> Ok(false)

Macros

Macros for value checking

Examples:

assert_lt!(1, 2); // check that 1 is less than 2
//-> Ok(true)
assert_lt!(2, 1);
//-> panic!("assertion failed: `assert_lt(left, right)`\n  left: `2`\n right: `1`")

Macros for function checking

To compare function return values:

assert_fn_eq!(i32::abs, 1, -1); // abs(1) == abs(-1)
//-> Ok(true)

To compare function Result Ok() values:

assert_fn_ok_eq!(i32::from_str, "1", "1"); // i32::from_str("1").unwrap() == i32::from_str("1").unwrap()
//-> Ok(true)

Test a function Result Err() strings:

assert_fn_err_string_eq!(i32::from_str, "foo", "goo"); // i32::from_str("foo").unwrap_err().to_string() == i32::from_str("goo").unwrap_err().to_string()
//-> Ok(true)

Two functions that are our favorites to use in our tests:

  • assert_fn_ok_eq!(i32::from_str, str1, str2); // compare parsed numbers

  • assert_fn_ok_eq!(::std::fs::read_to_string, file1, file2); // compare file text

Macros for set checking

These macros help with comparison of set parameters, such as two arrays or two vectors. where the item order does not matter, and the item count does not matter.

Examples:

assert_set_eq!([1, 2], [2, 1]);
//-> ()
assert_set_eq!([1, 2], [3, 4]);
//-> Err("assertion failed: `assert_set_eq(left, right)`\n  left: `[1, 2]`\n right: `[3, 4]`")

Macros for bag checking

Thes macros help with comparison of bag parameters, such as comparison of two arrays or two vectors, where the item order does not matter, and the item count does matter.

Examples:

assert_bag_eq!([1, 1], [1, 1]);
//-> ()
assert_bag_eq!([1, 1], [1, 1, 1]);
//-> Err("assertion failed: `assert_bag_eq(left, right)`\n  left: `[1, 1]`\n right: `[1, 1, 1]`")

These macros help with IO-related checking, such as comparison of files, streams, etc. These macros return a Result with Ok(true) or Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, message)).

Examples:

assert_io_lt!(1, 2);
//-> ()
assert_io_lt!(2, 1);
//-> panic!("assertion failed: `assert_io_lt(left, right)`\n  left: `2`\n right: `1`")

The return is especially helpful for the macro assume… error:

let x = assume_io_lt!(2, 1);
//-> Err(
//       std::io::Error::new(
//           std::io::ErrorKind::InvalidInput,
//           "assumption failed: `assume_io_lt(left, right)`\n  left: `2`\n right: `1`")]
//       )
//   )

Extras

Custom error messages

The macros have a second form where a custom error message can be provided.

Comparison abbreviations

The comparison macros use abbreviations such as eq (equals), ne (not equals), lt (less than), le (less than or equal to), gt (greater than), ge (greater than or equals).

Modules

assert
assert_bag_eq
assert_bag_ne
assert_eq
assert_fn_eq
assert_fn_err_string_eq
assert_fn_err_string_ge
assert_fn_err_string_gt
assert_fn_err_string_le
assert_fn_err_string_lt
assert_fn_err_string_ne
assert_fn_ge
assert_fn_gt
assert_fn_le
assert_fn_lt
assert_fn_ne
assert_fn_ok_eq
assert_fn_ok_ge
assert_fn_ok_gt
assert_fn_ok_le
assert_fn_ok_lt
assert_fn_ok_ne
assert_ge
assert_gt
assert_io_eq
assert_io_ge
assert_io_gt
assert_io_le
assert_io_lt
assert_io_ne
assert_le
assert_lt
assert_ne
assert_set_eq
assert_set_ne
assume
assume_bag_eq
assume_bag_ne
assume_eq
assume_fn_eq
assume_fn_err_string_eq
assume_fn_err_string_ge
assume_fn_err_string_gt
assume_fn_err_string_le
assume_fn_err_string_lt
assume_fn_err_string_ne
assume_fn_ge
assume_fn_gt
assume_fn_le
assume_fn_lt
assume_fn_ne
assume_fn_ok_eq
assume_fn_ok_ge
assume_fn_ok_gt
assume_fn_ok_le
assume_fn_ok_lt
assume_fn_ok_ne
assume_ge
assume_gt
assume_io
assume_io_eq
assume_io_ge
assume_io_gt
assume_io_le
assume_io_lt
assume_io_ne
assume_le
assume_lt
assume_ne
assume_set_eq
assume_set_ne
assure
assure_bag_eq
assure_bag_ne
assure_eq
assure_fn_eq
assure_fn_err_string_eq
assure_fn_err_string_ge
assure_fn_err_string_gt
assure_fn_err_string_le
assure_fn_err_string_lt
assure_fn_err_string_ne
assure_fn_ge
assure_fn_gt
assure_fn_le
assure_fn_lt
assure_fn_ne
assure_fn_ok_eq
assure_fn_ok_ge
assure_fn_ok_gt
assure_fn_ok_le
assure_fn_ok_lt
assure_fn_ok_ne
assure_ge
assure_gt
assure_io
assure_io_eq
assure_io_ge
assure_io_gt
assure_io_le
assure_io_lt
assure_io_ne
assure_le
assure_lt
assure_ne
assure_set_eq
assure_set_ne

Macros

assert_bag_eq

Assert two bags are equal.

assert_bag_ne

Assert two bags are not equal.

assert_fn_eq

Assert one function output is equal to another function output.

assert_fn_err_string_eq

Assert one function ok() is equal to another function ok().

assert_fn_err_string_ge

Assert one function ok() is greater than or equal to another function ok().

assert_fn_err_string_gt

Assert one function ok() is greater than another function ok().

assert_fn_err_string_le

Assert one function ok() is less than or equal to another function ok().

assert_fn_err_string_lt

Assert one function ok() is less than another function ok().

assert_fn_err_string_ne

Assert one function ok() is not equal to another function ok().

assert_fn_ge

Assert one function output is greater than or equal to another function output.

assert_fn_gt

Assert one function output is greater than another function output.

assert_fn_le

Assert one function output is less than or equal to another function output.

assert_fn_lt

Assert one function output is less than another function output.

assert_fn_ne

Assert one function output is not equal to another function output.

assert_fn_ok_eq

Assert one function ok() is equal to another function ok().

assert_fn_ok_ge

Assert one function ok() is greater than or equal to another function ok().

assert_fn_ok_gt

Assert one function ok() is greater than another function ok().

assert_fn_ok_le

Assert one function ok() is less than or equal to another function ok().

assert_fn_ok_lt

Assert one function ok() is less than another function ok().

assert_fn_ok_ne

Assert one function ok() is not equal to another function ok().

assert_ge

Assert one value is greater than or equal to another value.

assert_gt

Assert one value is greater than another value.

assert_io_eq

Assert two values are equal.

assert_io_ge

Assert one value is greater than or equal to another value.

assert_io_gt

Assert one value is greater than another value.

assert_io_le

Assert one value is less than or equal to another value.

assert_io_lt

Assert one value is less than another value.

assert_io_ne

Assert two values are equal.

assert_le

Assert one value is less than or equal to another value.

assert_lt

Assert one value is less than another value.

assert_set_eq

Assert two sets are equal.

assert_set_ne

Assert two sets are not equal.

assume

Assume a condition is true.

assume_bag_eq

Assume two bags are equal.

assume_bag_ne

Assume two bags are not equal.

assume_eq

Assume two values are equal.

assume_fn_eq

Assume one function output is equal to another function output.

assume_fn_err_string_eq

Assume one function ok() is equal to another function ok().

assume_fn_err_string_ge

Assume one function ok() is not equal to another function ok().

assume_fn_err_string_gt

Assume one function ok() is greater than another function ok().

assume_fn_err_string_le

Assume one function ok() is less than or equal to another function ok().

assume_fn_err_string_lt

Assume one function ok() is less than another function ok().

assume_fn_err_string_ne

Assume one function ok() is not equal to another function ok().

assume_fn_ge

Assume one function output is not equal to another function output.

assume_fn_gt

Assume one function output is greater than another function output.

assume_fn_le

Assume one function output is less than or equal to another function output.

assume_fn_lt

Assume one function output is less than another function output.

assume_fn_ne

Assume one function output is not equal to another function output.

assume_fn_ok_eq

Assume one function ok() is equal to another function ok().

assume_fn_ok_ge

Assume one function ok() is not equal to another function ok().

assume_fn_ok_gt

Assume one function ok() is greater than another function ok().

assume_fn_ok_le

Assume one function ok() is less than or equal to another function ok().

assume_fn_ok_lt

Assume one function ok() is less than another function ok().

assume_fn_ok_ne

Assume one function ok() is not equal to another function ok().

assume_ge

Assume one value is greater than or equal to another value.

assume_gt

Assume one value is greater than another value.

assume_io

Assume a condition is true.

assume_io_eq

Assume one value is equal to another value.

assume_io_ge

Assume one value is greater than or equal to another value.

assume_io_gt

Assume one value is greater than another value.

assume_io_le

Assume one value is less than or equal to another value.

assume_io_lt

assume one value is less than another value.

assume_io_ne

Assume one value is not equal to another value.

assume_le

Assume one value is less than or equal to another value.

assume_lt

Assume one value is less than another value.

assume_ne

Assume two values are not equal.

assume_set_eq

Assume two sets are equal.

assume_set_ne

Assume two sets are not equal.

assure

Assure a condition is true.

assure_bag_eq

Assure two bags are equal.

assure_bag_ne

Assure two bags are not equal.

assure_eq

Assure two values are equal.

assure_fn_eq

Assure one function output is equal to another function output.

assure_fn_err_string_eq

Assure one function ok() is equal to another function ok().

assure_fn_err_string_ge

Assure one function ok() is greater than or equal to another function ok().

assure_fn_err_string_gt

Assure one function ok() is greater than another function ok().

assure_fn_err_string_le

Assure one function ok() is less than or equal to another function ok().

assure_fn_err_string_lt

Assure one function ok() is less than to another function ok().

assure_fn_err_string_ne

Assure one function ok() is not equal to another function ok().

assure_fn_ge

Assure one function output is greater than or equal to another function output.

assure_fn_gt

Assure one function output is greater than another function output.

assure_fn_le

Assure one function output is less than or equal to another function output.

assure_fn_lt

Assure one function output is less than to another function output.

assure_fn_ne

Assure one function output is not equal to another function output.

assure_fn_ok_eq

Assure one function ok() is equal to another function ok().

assure_fn_ok_ge

Assure one function ok() is greater than or equal to another function ok().

assure_fn_ok_gt

Assure one function ok() is greater than another function ok().

assure_fn_ok_le

Assure one function ok() is less than or equal to another function ok().

assure_fn_ok_lt

Assure one function ok() is less than to another function ok().

assure_fn_ok_ne

Assure one function ok() is not equal to another function ok().

assure_ge

Assure one value is greater than or equal to another value.

assure_gt

Assure one value is greater than another value.

assure_io

Assure a condition is true.

assure_io_eq

Assure one value is equal to another value.

assure_io_ge

Assure one value is greater than or equal to another value.

assure_io_gt

Assure one value is greater than another value.

assure_io_le

Assure one value is less than or equal to another value.

assure_io_lt

Assure one value is less than another value.

assure_io_ne

Assure one value is not equal to another value.

assure_le

Assure one value is less than or equal to another value.

assure_lt

Assure one value is less than another value.

assure_ne

Assure two values are not equal.

assure_set_eq

Assure two sets are equal.

assure_set_ne

Assure two sets are not equal.